function deleteEmp(no) {
    window.location.href="deleteEmp?empno="+no;
}
function updateEmp(no) {
    window.location.href="updateEmp?empno="+no;
}

xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = showData;//设置回调函数：服务器返回了数据，哪个函数来负责处理数据

function currentPage(currentPage,totalPage,x) {


    /*在jsp页面中可以获取数据，在js页面不可以
    var currentPage = "${requestScope.pm.currentPage}";
    var totalPage = "${requestScope.pm.totalPage}";
    currentPage = Number(currentPage);
    totalPage = Number(totalPage);*/

    var page;
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
        var data = JSON.parse(xmlHttp.responseText);
        currentPage = data.currentPage;
    }

    var pageFirst = document.getElementById("homePage");
    var pagePrevious = document.getElementById("prePage");
    var pageNext = document.getElementById("nextPage");
    var pageEnd = document.getElementById("endPage");

    switch (x) {
        case 1:
            if (currentPage == 1) {
                page = currentPage;
                pageFirst.disabled = "disabled";
                pagePrevious.disabled = "disabled";
                pageNext.disabled = "";
                pageEnd.disabled = "";
            } else {
                page = 1;
                pageFirst.disabled = "disabled";
                pagePrevious.disabled = "disabled";
                pageNext.disabled = "";
                pageEnd.disabled = "";
            }
            break;
        case 2:
            if (currentPage == 1) {
                page = currentPage;
                pageFirst.disabled = "disabled";
                pagePrevious.disabled = "disabled";
                pageNext.disabled = "";
                pageEnd.disabled = "";
            } else {
                page = currentPage - 1;
                pageFirst.disabled = "";
                pagePrevious.disabled = "";
                pageNext.disabled = "";
                pageEnd.disabled = "";
            }
            break;
        case 3:
            if (currentPage == totalPage) {
                page = currentPage;
                pageFirst.disabled = "";
                pagePrevious.disabled = "";
                pageNext.disabled = "disabled";
                pageEnd.disabled = "disabled";
            } else {
                page = currentPage + 1;
                pageFirst.disabled = "";
                pagePrevious.disabled = "";
                pageNext.disabled = "";
                pageEnd.disabled = "";
            }
            break;
        case 4:
            if (currentPage != totalPage) {
                page = totalPage;
                pageFirst.disabled = "";
                pagePrevious.disabled = "";
                pageNext.disabled = "disabled";
                pageEnd.disabled = "disabled";
            } else {
                page = totalPage;
                pageFirst.disabled = "";
                pagePrevious.disabled = "";
                pageNext.disabled = "disabled";
                pageEnd.disabled = "disabled";
            }
            break;
    }
    xmlHttp.open("GET", "viewEmp?page=" + page);//打开连接

    xmlHttp.send(null);//发送请求


}
function showData() {
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
        var span = document.getElementById("span");//标签插入”第x页/共n页“
        var table = document.getElementById("empVTable"); //定位到table上
        var tbody_old = document.getElementsByTagName("tbody")[1];//
        table.removeChild(tbody_old);

        var data = JSON.parse(xmlHttp.responseText);
        var newtbody = document.createElement("tbody");
        for (prop in data) {
            if (prop = 'list') {
                for (p in data[prop]) {
                    var empNo = data[prop][p].empno;
                    var tr = document.createElement("tr");

                    var td1 = document.createElement("td");
                    var td2 = document.createElement("td");
                    var td3 = document.createElement("td");//给删除和更新的链接

                    var a_delete = document.createElement("a");
                    a_delete.setAttribute("href", "javascript:void(0)");
                    a_delete.setAttribute("onclick", "delete_data(" + empNo + ")");
                    a_delete.innerText = "删除  ";
                    td3.appendChild(a_delete);
                    var a_update = document.createElement("a");
                    a_update.setAttribute("href", "javascript:void(0)");
                    a_update.setAttribute("onclick", "update_data(" + empNo + ")");
                    a_update.innerText = "修改";
                    td3.appendChild(a_update);

                    td1.innerHTML = data[prop][p].empno;
                    td2.innerHTML = data[prop][p].ename;
                    td3.innerHTML = data[prop][p].deptno;

                    tr.appendChild(td1);
                    tr.appendChild(td2);
                    tr.appendChild(td3);
                    newtbody.appendChild(tr);
                }
                table.appendChild(newtbody);
                break;
            }
        }
        span.innerHTML = "&nbsp;&nbsp;第" + data.currentPage + "页" + "/共" + data.totalPage + "页&nbsp;&nbsp;";
    }
}
//page翻页中js函数



//传统方法分页
/*
function homePage(n) {
    if(n==1){
        var obj = document.getElementById('homePage');
        obj.className +=  ' disabled' ;
        // $("tr").removeClass("styleclass");移除类
    }else{
        //window.location.href="emp?page="+1;
        xmlHttp=new XMLHttpRequest();
        xmlHttp.onreadystatechange=showData;
        xmlHttp.open("GET","viewEmp?page=1");
        xmlHttp.send(null);
    }

}

function homePage(n) {

    //window.location.href="emp?page="+1;
    xmlHttp=new XMLHttpRequest();
    xmlHttp.onreadystatechange=showData;
    xmlHttp.open("GET","viewEmp?page=1");
    xmlHttp.send(null);


}

function prePage(n) {
    if(n==1){
        var obj = document.getElementById('prePage');
        obj.className +=  ' disabled' ;
        // $("tr").removeClass("styleclass");移除类
    }else{
        window.location.href="emp?page="+(n-1);
    }

}
function nextPage(r,n) {
    if(r==n){
        var obj = document.getElementById('nextPage');
        obj.className +=  ' disabled' ;
        // $("tr").removeClass("styleclass");移除类
    }else{
        window.location.href="emp?page="+(r+1);
    }

}
function endPage(c,n) {
    if(r==n){
        var obj = document.getElementById('endPage');
        obj.className +=  ' disabled' ;
        // $("tr").removeClass("styleclass");移除类
    }else{
        window.location.href="emp?page="+n;
    }

}*/